home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- * frommerc -
- * Convert from a mercator projection to an environment map.
- *
- * Paul Haeberli - 1990
- */
- #include "texture.h"
-
- #define XSIZE (3*size)
- #define YSIZE (2*size)
- #define HACK (0.001)
-
- TEXTURE *tm;
- int size, samples;
-
- main(argc,argv)
- int argc;
- char **argv;
- {
- float vr, vg, vb;
- float tr, tg, tb;
- int x, y;
- IMAGE *image;
- int i;
- vect c, pos, dir, merc;
- float temp;
- short *rbuf, *gbuf, *bbuf;
-
- if(argc<5) {
- fprintf(stderr,"usage: frommerc size samples file.rgb file.env\n");
- exit(1);
- }
- size = atoi(argv[1]);
- samples = atoi(argv[2]);
- tm = tmopen(argv[3]);
- if(!tm) {
- fprintf(stderr,"can't open input image.\n");
- exit(1);
- }
- rbuf = (short *)malloc(XSIZE*sizeof(short));
- gbuf = (short *)malloc(XSIZE*sizeof(short));
- bbuf = (short *)malloc(XSIZE*sizeof(short));
- image = iopen(argv[4],"w",RLE(1),3,XSIZE,YSIZE,3);
- for(y=0; y<YSIZE; y++) {
- for(x=0; x<XSIZE; x++) {
- tr = tg = tb = 0.0;
- for(i=0; i<samples; i++) {
- if(samples>1) {
- pos.x = (x+frand())/(XSIZE+HACK);
- pos.y = (y+frand())/(YSIZE+HACK);
- pos.z = 0;
- } else {
- pos.x = (x+0.5)/XSIZE;
- pos.y = (y+0.5)/YSIZE;
- pos.z = 0.0;
- }
- envtovect(&pos,&dir);
- #ifdef HORIZON
- temp = dir.x;
- dir.x = dir.y;
- dir.y = dir.z;
- dir.z = temp;
- #endif
- dirtomerc(&dir,&merc);
- tmsample(tm,&merc,&c);
- tr += c.x;
- tg += c.y;
- tb += c.z;
- }
- rbuf[x] = 255*tr/samples;
- gbuf[x] = 255*tg/samples;
- bbuf[x] = 255*tb/samples;
- }
- putrow(image,rbuf,y,0);
- putrow(image,gbuf,y,1);
- putrow(image,bbuf,y,2);
- tpercentdone(100.0*y/(YSIZE-1));
- }
- iclose(image);
- }
-
- dirtomerc(dir,merc)
- vect *dir, *merc;
- {
- float magout, r;
-
- r = sqrt(dir->x*dir->x+dir->y*dir->y);
- merc->x = (atan2(dir->x,dir->y)/(2*M_PI))+0.5;
- merc->y = (atan2(r,dir->z)/M_PI);
- merc->z = 0.0;
- return 1;
- }
-